-- 把Utility的一些函数进行了改造，有些也许会提出合并申请？

---@class Th_Utility
local Th_Utility = require 'packages/2hu/pkg/util/_base'

--- 获得一张**实体牌**最后一次离开非处理区进入非角色区域时位于谁的区域
---@param room Room @ 房间
---@param card Card | integer @ **实体牌**或其ID
---@return ServerPlayer? @ 最后区域所有者，若无则表示此牌从未如此离开过
Th_Utility.getCardLastOwner = function(room, card)
  local logic = room.logic
  local cid = Card:getIdList(card)[1]
  local last_owner = room:getCardOwner(cid) ---@cast last_owner ServerPlayer
  if last_owner then return last_owner end
  logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.to == nil then
        for _, info in ipairs(move.moveInfo) do
          if info.cardId == cid and info.fromArea ~= Card.Processing then
            last_owner = move.from
            return true
          end
        end
      end
    end
  end, 0)
  return last_owner
end

return Th_Utility
